package com.demo.okhttp

import okhttp3.*
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody.Companion.asRequestBody
import java.io.File
import java.security.cert.X509Certificate
import javax.net.ssl.HostnameVerifier
import javax.net.ssl.SSLSession
import javax.net.ssl.X509TrustManager

class InterceptorAddDemo {

    companion object {
        val MEDIA_TYPE_MARKDOWN = "text/x-markdown; charset=utf-8".toMediaType()
    }

    fun testInterceptor() {
        val client = OkHttpClient.Builder()
//                .addInterceptor(LoggingInterceptor())
                .addNetworkInterceptor(LoggingInterceptor())
                .hostnameVerifier(object : HostnameVerifier {
                    override fun verify(p0: String?, p1: SSLSession?): Boolean {
                        return true
                    }

                })
                .cache(Cache(File("./cache"), 10240))
                .build()

        val request = Request.Builder()
                .url("http://www.publicobject.com/helloworld.txt")
                .header("User-Agent", "OkHttp Example")
                .build()

        val response = client.newCall(request).execute()
        response.body?.close()
    }
}

fun main() {
    val interceptorAddDemo = InterceptorAddDemo()
    interceptorAddDemo.testInterceptor()
}